﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

public partial class playMusic : System.Web.UI.Page
{
    //设置全局变量用来存储歌曲路径
    public string fileUrl;

    //设置全局变量用来存储歌曲名称
    public string fileName;

    //设置全局变量用来存储歌曲的歌词
    public string str;

    //设置全局变量用来存储歌词中“]”总数
    public int sysum;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //创建SQL语句，根据传入的歌曲id更新歌曲的试听次数
            string sqlUp = "update tb_musicInfo set auditionSum=auditionSum+1 where id=" + Request["id"];
            dataoperate.execSql(sqlUp);
            //创建SQL语句，根据歌曲的id查询歌曲信息
            string sqlSel = "select * from tb_musicInfo where id=" + Request["id"];
            //调用公共类中的getRow方法并接收该方法返回的对象
            SqlDataReader sdr = dataoperate.getRow(sqlSel);
            //读取一条记录
            sdr.Read();
            //获取歌曲的路径
            fileUrl = setUrl("musicFile/" + sdr["musicPath"]);
            
            //获取歌曲的名称
            fileName = sdr["musicName"].ToString();       
            //获取歌词
            str = File.ReadAllText(Server.MapPath("musicFile//" + sdr["lyricPath"]), System.Text.Encoding.GetEncoding("gb2312"));
            //设置显示歌词的div
            Literal1.Text += "<div align='center'  id='lrcAreaDiv' style='height:60px; width:350px;overflow:hidden;'>";
            Literal1.Text += "<table border='0' cellspacing='0' cellpadding='0' id='lrcArea' width='100%' style='z-index:-1;position:relative; top:120px;'>";
            Literal1.Text += "<tr><td nowrap height='20' align='center'>";
            Literal1.Text += "<table border='0' cellspacing='0' cellpadding='0'>";
            Literal1.Text += "<tr><td nowrap height='20'><span id='lrcLine1' style='height:20; color:#FF0000'>正在加载歌词……</span></td></tr>";
            Literal1.Text += "<tr style='position:relative; top: -20px; z-index:6;'>";
            Literal1.Text += "<td nowrap height='20'><div id='lrcLine_will1' class='lrcLine_will'></div></td></tr></table>";
            Literal1.Text += "</td></tr>";
            //获取歌词中“[”总数
            sysum = getStr(sdr["lyricPath"].ToString());
            //循环添加表格
            for (int i = 0; i < getStr(sdr["lyricPath"].ToString()); i++)
            {
                Literal1.Text += "<tr style='position:relative; top: " + -20 * i + "px;'><td nowrap height='20' align='center'>";
                Literal1.Text += "<table border='0' cellspacing='0' cellpadding='0'>";
                Literal1.Text += "<tr><td nowrap height='20'><span id='lrcLine" + (i + 2) + "' style='height:20'></span></td></tr>";
                Literal1.Text += "<tr style='position:relative; top: -20px; z-index:6;'>";
                Literal1.Text += "<td nowrap height='20'><div id='lrcLine_will" + (i + 2) + "' class='lrcLine_will'></div></td>";
                Literal1.Text += "</tr></table></td></tr>";
            }            
            Literal1.Text += "</table></div>";
        }



    }
    
    public string setUrl(string url)
    {
        //获取当前的绝对路径
        string sss = Request.Url.AbsoluteUri;
        //查询play.aspx在字符串中的位置
        int idx = sss.IndexOf("playMusic.aspx");
        //获取指定字符串
        sss = sss.Substring(0, idx);
        return sss + url;

    }


    protected int getStr(string lrcPath)
    {
        //获取歌词
        string s = File.ReadAllText(Server.MapPath("musicFile//"+lrcPath), System.Text.Encoding.GetEncoding("gb2312"));
        //将歌词已“[”分隔存储到数组中
        string[] ss = s.Split('[');
        //返回数组的长度
        return ss.Length;

    }


}
